package cn.edu.neusoft.food729.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.retroiftdemo_729.R;

import cn.edu.neusoft.food729.Bean.LoginBean;
import cn.edu.neusoft.food729.listener.LoginListener;
import cn.edu.neusoft.food729.model.LoginModel;

public class LoginActivity extends AppCompatActivity {

    private EditText etuser, etpass;
    private Button btlogin,btzhuce;
    private Switch jzmm;
    private String username = "", password = "", sessionID = "";
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getUserid();
            Log.i("LoginActivity", "----sessionID=" + sessionID);
            if (sessionID != null) {
                if (jzmm.isChecked()) {
                    saveSP(etuser.getText().toString(),
                            etpass.getText().toString(),
                            sessionID,
                            jzmm.isChecked());
                }
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                saveSP("","","",false);
                Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            saveSP("","","",false);
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };
        private void saveSP(String name, String pass, String session, Boolean state) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("name",name);
            editor.putString("pass",pass);
            editor.putBoolean("state",state);
            editor.putString("session",session);
            editor.commit();
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {//沉浸式状态栏
//            ImmersionBar.with(this).statusBarColor(R.color.transparent)
//                    .fitsSystemWindows(true).init();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            init();
            sp = getSharedPreferences("login", MODE_PRIVATE);
            readSP();
        }

    private void readSP() {
        String name = sp.getString("name","");
        String pass = sp.getString("pass","");
        Boolean state = sp.getBoolean("state",false);
        if(state){
            etuser.setText(name);
            etpass.setText(pass);
        }
        jzmm.setChecked(state);
    }

    private void init() {
        etuser = findViewById(R.id.editText);
        etpass = findViewById(R.id.editText2);
        btlogin = findViewById(R.id.button);
        btzhuce = findViewById(R.id.button2);
        jzmm=findViewById(R.id.switch1);

        btlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                if (username.length() == 0 || password.length() == 0) {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    LoginModel loginModel = new LoginModel();
                    loginModel.getLoginResult(username, password, loginListener);
                }
            }
        });
        btzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

    }

}
